// 导入库
var storage = storages.create("autotouchShijieXy");
// 固定数据
var gamePackageName = "com.qqgame.mic";
var floatyConsole; // 日志悬浮窗
var yanchi = 500; // 点击后停顿
var yanchi1 = 1000; // 点击前停顿
var yanchi2 = 2000; // 点击后停顿
var yanchi3 = 3000; // 点击后停顿
var yanchi5 = 5000; // 点击后停顿
var deviceWidth = device.width > device.height ? device.width : device.height;
var deviceHeight = device.height > device.width ? device.width : device.height;
var picJizhun = deviceHeight / 1080;
if (deviceHeight == 720) {
    picJizhun = 0.666666666666667;
}
if (deviceHeight == 1200) {
    picJizhun = 1.12;
}
var picJizhunHeight = picJizhun;
log('基准数据：', deviceWidth, deviceHeight, picJizhun)
var myLogStr = ""; // 重写打印
log('初始化数据完成')

// 系统函数
function myLogFunc() {
    log(arguments)
    myLogStr += '\n' + String(arguments[0])
    //对控件的操作需要在UI线程中执行
    ui.run(function () {
        floatyConsole.myLog.text(" " + myLogStr + " ");
    });
    let zanting = storage.get("zanting", false);
    while (zanting) {
        zanting = storage.get("zanting", false);
        sleep(2000)
    }
}

// 监听本脚本运行结束
events.on("exit", function () {
    log("监听本脚本运行结束")
    if (floatyConsole) {
        floatyConsole.close()
    }
    floaty.closeAll()
});

//监听 say 事件
events.on("say", function (words) {
    if (words) {
        floatyConsole.close();
        exit()
    }
});

// 悬浮窗
function xuanfuLogFunc() {
    floatyConsole = floaty.rawWindow(
        <frame gravity="left">
            <text id="myLog" text="日志" textColor="#ffffff" textSize="8sp" ellipsize="marquee" gravity="bottom" />
        </frame>
    );
    let w = 358 * picJizhun
    let h = 188 * picJizhun
    floatyConsole.setSize(w, h)
    floatyConsole.setPosition(5, device.width - h);
    floatyConsole.setTouchable(false)
    floatyConsole.myLog.text(" " + myLogStr + " ");
}


// 脚本自执行
mainFunc();
function mainFunc() {
    log("开始任务")
    // 检查无障碍服务是否已经启用，如果没有启用则跳转到无障碍服务启用界面，并等待无障碍服务启动；当无障碍服务启动后脚本会继续运行。
    auto.waitFor()
    log("截图权限")
    //截图权限自动化安卓版本高于Android 9
    if (device.sdkInt > 27) {
        //等待截屏权限申请并同意
        threads.start(function () {
            if (device.brand == "HUAWEI") {
                packageName('com.android.systemui').text('允许').waitFor();
                sleep(yanchi)
                text('允许').click();
            } else {
                packageName('com.android.systemui').text('立即开始').waitFor();
                sleep(yanchi)
                text('立即开始').click();
            }
        });
    }
    sleep(yanchi)
    //申请截屏权限
    if (!requestScreenCapture(true)) {
        toastLog("请求截图失败");
        exit()
    }
    xuanfuLogFunc()
    sleep(yanchi1)
    // 启动游戏
    let appPackName = currentPackage();
    myLogFunc("启动游戏", appPackName)
    app.launchPackage(gamePackageName);
    myLogFunc("将游戏调整至关卡页,稍等...")
    let renwu = storage.get("xunhuanrenwu", 'shuayangzhou')
    sleep(5000)
    if (renwu == 'shuayangzhou') {
        dianhanxinFunc();
    } else {
        diantongguanFunc();
    }
    
}


// 点探索
function diantongguanFunc() {
    let thisImgs = null;
    thisImgs = images.read("./res/tongguantansuo.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.86 });
    thisImgs.recycle();
    if (p) {
        click(p.x, p.y)
        myLogFunc('点击探索')
        sleep(yanchi)
        tansuolaFunc()
    } else {
        myLogFunc('等探索_2s')
        sleep(yanchi2)
        diantongguanFunc()
    }
}

function tansuolaFunc() {
    let thisImgs = images.read("./res/tansuo.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.85 });
    thisImgs.recycle();
    if (p) {
        click(p.x, p.y)
        myLogFunc("探索啊")
        sleep(yanchi1)
        ts_xiadanFunc()
    }
}

function ts_xiadanFunc() {
    let thisImgs = images.read("./res/hx_tansuoqueren.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.85 });
    thisImgs.recycle();
    if (p) {
        click(p.x, p.y)
        myLogFunc("点击确认")
    }

    sleep(yanchi)

    let thisImgs1 = images.read("./res/hx_tongguanqueding.png");
    thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
    let p1 = findImage(captureScreen(), thisImgs1, { threshold: 0.83 });
    thisImgs1.recycle();
    if (p1) {
        click(p.x, p.y)
        myLogFunc('点击确定')
        sleep(yanchi)
        diantongguanFunc()
    } else {
        log('没有确定')
        sleep(yanchi1)
        diantongguanFunc()
    }
}

// 刷林冲函数 点关卡头像
function dianhanxinFunc() {
    let thisImgs = null; // 韩信-hx_hanxin 刘邦-hx_liubang 吴三桂-hx_wusangui 项羽-hx_xiangyu
    let yangzhouShuia = storage.get("yangzhouShuia", '0')
    log(yangzhouShuia)
    if (yangzhouShuia == 0) {
        thisImgs = images.read("./res/hx_bubinger.png");
    }
    if (yangzhouShuia == 1) {
        thisImgs = images.read("./res/hx_gongbinger.png");
    }
    if (yangzhouShuia == 2) {
        thisImgs = images.read("./res/hx_hanxin.png");
    }
    if (yangzhouShuia == 3) {
        thisImgs = images.read("./res/hx_liubang.png");
    }
    if (yangzhouShuia == 4) {
        thisImgs = images.read("./res/hx_xiangyu.png");
    }
    if (yangzhouShuia == 5) {
        thisImgs = images.read("./res/hx_lvzhi.png");
    }
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.86 });
    thisImgs.recycle();
    if (p) {
        log("PP:",p)
        click(p.x+10, p.y+28)
        myLogFunc('进去关卡')
        sleep(yanchi)
        sleep(yanchi1)
        xuanlemei()
    } else {
        myLogFunc('等关卡_2s')
        sleep(yanchi2)
        dianhanxinFunc()
    }
}
function xuanlinchong() {
    let thisImgs = images.read("./res/hx_linchong.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.81 });
    thisImgs.recycle();
    if (p) {
        click(p.x, p.y)
        sleep(yanchi)
    }
}
function xuanlemei() {
    let thisImgs = images.read("./res/hx_xuanzhujiang.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    // let p = findImage(captureScreen(), thisImgs);
    let result = images.matchTemplate(captureScreen(), thisImgs, { region: [0, 0, deviceWidth, (deviceHeight / 2)], threshold: 0.82, max: 2 });
    thisImgs.recycle();
    if (result.matches.length) {
        myLogFunc("进行选将")
        xuanlinchong()
        let thisImgsElse = images.read("./res/英雄信息.png");
        thisImgsElse = images.scale(thisImgsElse, picJizhun, picJizhunHeight)
        let resultRc = images.matchTemplate(captureScreen(), thisImgsElse, { region: [0, (deviceHeight / 3), deviceWidth, (deviceHeight / 2)], threshold: 0.8, max: 8 });
        let matchRc = resultRc.sortBy("left-right").matches;
        for (let index = 0; index < matchRc.length; index++) {
            click(matchRc[index].point.x - 60, matchRc[index].point.y + 100);
            sleep(yanchi1)
            if (index > 3) {
                break;
            }
        }
        xuanlemei()
    } else {
        kaizhanla()
    }
}
function kaizhanla() {
    let thisImgs = images.read("./res/hx_kaizhan.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.85 });
    thisImgs.recycle();
    if (p) {
        click(p.x, p.y)
        myLogFunc("开战")
        sleep(yanchi)
        sleep(yanchi1)
        hx_xiadanFunc()
    }
}
function chupaiquedingFunc(duoyanchi) {
    sleep(yanchi)
    let thisImgs2 = images.read("./res/hx_queding.png");
    thisImgs2 = images.scale(thisImgs2, picJizhun, picJizhunHeight)
    let p2 = findImage(captureScreen(), thisImgs2, { threshold: 0.85 });
    thisImgs2.recycle();
    if (p2) {
        click(p2.x, p2.y)
        myLogFunc('确定出牌')
        if (duoyanchi) {
            sleep(yanchi3)
        } else {
            sleep(yanchi2)
        }
    }
}

function chupai1Func() {
    let cap = captureScreen()
    let thisImgs = images.read("./res/hx_shnegyou.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(cap, thisImgs, { region: [0, (deviceHeight / 2), deviceWidth, (deviceHeight / 2)], threshold: 0.85 });
    thisImgs.recycle();
    if (p) {
        click(p.x, p.y)
        myLogFunc("无中生有")
        chupaiquedingFunc(true)
    }
}
function chupai2Func() {
    let cap = captureScreen()
    let thisImgs = images.read("./res/hx_hufu.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(cap, thisImgs, { region: [0, (deviceHeight / 2), deviceWidth, (deviceHeight / 2)], threshold: 0.85 });
    thisImgs.recycle();
    if (p) {
        click(p.x, p.y)
        myLogFunc("虎符")
        chupaiquedingFunc()
    }
}
function hx_xiadanFunc() {
    let thisImgs = images.read("./res/hx_xiadan.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.85 });
    thisImgs.recycle();
    if (p) {
        // 可以出来
        chupai1Func()
        chupai2Func()
        click(p.x, p.y)
        myLogFunc("点击侠胆")
        sleep(yanchi1)
        let thisImgs1 = images.read("./res/hx_pindianyx.png");
        thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
        let p1 = findImage(captureScreen(), thisImgs1, { threshold: 0.63 });
        thisImgs1.recycle();
        if (p1) {
            log('可以拼点')
            hx_youjunFunc()
        } else {
            log('无法拼点')
        }
    } else {
        sleep(yanchi)
        log("主动技能")
        let thisImgs = images.read("./res/hx_queding.png");
        thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
        let p = findImage(captureScreen(), thisImgs, { region: [0, (deviceHeight / 2), deviceWidth, (deviceHeight / 2)], threshold: 0.85 });
        thisImgs.recycle();
        if (p) {
            click(p.x, p.y)
            myLogFunc("主动技能")
            sleep(yanchi1)
            hx_xiadanFunc()
        } else {
            sleep(yanchi)
            myLogFunc("等待")
            hx_xiadanFunc()
        }
    }
}
function qipaiFunc() {
    let thisImgs1 = images.read("./res/hx_qipai.png");
    thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
    let p1 = findImage(captureScreen(), thisImgs1, { threshold: 0.65 });
    thisImgs1.recycle();
    if (p1) {
        log('需要弃牌')
        let thisImgs = images.read("./res/hx_meihua.png");
        thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
        let result = images.matchTemplate(captureScreen(), thisImgs, { region: [0, (deviceHeight / 2), deviceWidth, (deviceHeight / 2)], threshold: 0.8, max: 6 });
        thisImgs.recycle();
        log('hx_meihua', result.matches)
        if (result.matches.length) {
            myLogFunc('选择弃牌')
            let matchRc = result.matches;
            for (let index = 0; index < matchRc.length; index++) {
                click(matchRc[index].point.x, matchRc[index].point.y);
                sleep(yanchi)
            }
            sleep(yanchi1)
            let thisImgs2 = images.read("./res/hx_queding.png");
            thisImgs2 = images.scale(thisImgs2, picJizhun, picJizhunHeight)
            let p2 = findImage(captureScreen(), thisImgs2, { threshold: 0.85 });
            thisImgs2.recycle();
            if (p2) {
                click(p2.x, p2.y)
                myLogFunc('确定弃牌')
                sleep(yanchi1)
            }
        } else {
            qipaiFangpian()
        }
    } else {
        log('无需弃牌')
    }
}
function qipaiFangpian() {
    let thisImgs = images.read("./res/hx_pianzi.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let result = images.matchTemplate(captureScreen(), thisImgs, { region: [0, (deviceHeight / 2), deviceWidth, (deviceHeight / 2)], threshold: 0.8, max: 6 });
    thisImgs.recycle();
    log('hx_pianzi', result.matches)
    if (result.matches.length) {
        myLogFunc('选择弃牌方片')
        let matchRc = result.matches;
        for (let index = 0; index < matchRc.length; index++) {
            click(matchRc[index].point.x, matchRc[index].point.y);
            sleep(yanchi)
        }
        sleep(yanchi1)
        let thisImgs2 = images.read("./res/hx_queding.png");
        thisImgs2 = images.scale(thisImgs2, picJizhun, picJizhunHeight)
        let p2 = findImage(captureScreen(), thisImgs2, { threshold: 0.85 });
        thisImgs2.recycle();
        if (p2) {
            click(p2.x, p2.y)
            myLogFunc('确定弃牌')
            sleep(yanchi1)
        }
    } else {
        myLogFunc('等待自动弃牌')
    }
}
function hx_xiadanFunc11() {
    myLogFunc("等待1")
    sleep(yanchi1)
    // 找侠胆
    let thisImgs = images.read("./res/hx_xiadan.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.85 });
    thisImgs.recycle();
    if (p) {
        // 可以出来
        chupai1Func()
        chupai2Func()
        click(p.x, p.y)
        myLogFunc("点击侠胆")
        sleep(yanchi1)
        let thisImgs1 = images.read("./res/hx_pindianyx.png");
        thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
        let p1 = findImage(captureScreen(), thisImgs1, { threshold: 0.65 });
        thisImgs1.recycle();
        if (p1) {
            log('可以拼点1')
            hx_youjunFunc()
        } else {
            log('无法拼点1')
        }
    } else {
        chupaiquedingFunc()
        sleep(yanchi2)
        myLogFunc("等待2")
        let hx_baoxiang = images.read("./res/hx_baoxiang.png");
        hx_baoxiang = images.scale(hx_baoxiang, picJizhun, picJizhunHeight)
        let phx = findImage(captureScreen(), hx_baoxiang, { threshold: 0.85 });
        hx_baoxiang.recycle();
        if (phx) {
            click(phx.x, phx.y)
            myLogFunc('宝箱2')
            sleep(yanchi)
            fanhuiaFunc()
        } else {
            let hx_fanhui = images.read("./res/hx_fanhui.png");
            hx_fanhui = images.scale(hx_fanhui, picJizhun, picJizhunHeight)
            let pfh = findImage(captureScreen(), hx_fanhui, { threshold: 0.85 });
            hx_fanhui.recycle();
            if (pfh) {
                click(pfh.x, pfh.y)
                myLogFunc('返回')
                sleep(yanchi1)
                myLogFunc('重新开始')
                dianhanxinFunc()
            } else {
                sleep(yanchi1)
                hx_xiadanFunc11()
            }
        }
    }
}
function hx_youjunFunc() {
    let thisImgs = images.read("./res/hx_youjun.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { region: [0, 0, deviceWidth, (deviceHeight / 2)], threshold: 0.85 });
    thisImgs.recycle();
    if (p) {
        log('p', p)
        click(p.x + 50, p.y + 100)
        myLogFunc("选取友军1")
        sleep(yanchi1)
        let thisImgs1 = images.read("./res/hx_queding.png");
        thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
        let p1 = findImage(captureScreen(), thisImgs1, { threshold: 0.85 });
        thisImgs1.recycle();
        if (p1) {
            click(p1.x, p1.y)
            myLogFunc('确定')
            sleep(yanchi1)
            xuanpai(13)
        }
    }
}
function xuanpai(num) {
    log(num)
    let imgStr = "./res/hx_pai_hei_" + num + ".png";
    let thisImgs = images.read(imgStr);
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.85 });
    if (p) {
        click(p.x, p.y)
        myLogFunc('选取较大牌' + num)
        sleep(yanchi1)

        let thisImgs0 = images.read("./res/hx_youjun.png");
        thisImgs0 = images.scale(thisImgs0, picJizhun, picJizhunHeight)
        let p0 = findImage(captureScreen(), thisImgs0, { threshold: 0.85 });
        thisImgs0.recycle();
        if (p0) {
            log('p0', p0)
            click(p0.x + 100, p0.y + 100)
            myLogFunc('选取友军2')
            sleep(yanchi1)

            let thisImgs1 = images.read("./res/hx_queding.png");
            thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
            let p1 = findImage(captureScreen(), thisImgs1, { threshold: 0.85 });
            thisImgs1.recycle();
            if (p1) {
                click(p1.x, p1.y)
                myLogFunc('确定2')
                sleep(yanchi1)

                hx_shaaFunc()
                sleep(yanchi1)
                myLogFunc('杀了1')
                // 第二次杀等待
                sleep(yanchi3)
                hx_shaaFunc()
                myLogFunc('杀了2')
                jieshuchupaiFunc()
            }
        } else {
            myLogFunc('选取友军失败')
        }


    } else {
        num = num - 1
        xuanpai(num)
    }
}

function hx_shaaFunc() {
    let thisImgs = images.read("./res/hx_shaa.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let result = images.matchTemplate(captureScreen(), thisImgs, { region: [0, (deviceHeight / 2), deviceWidth, (deviceHeight / 2)], threshold: 0.8, max: 6 });
    thisImgs.recycle();
    log('result.matches', result.matches)
    if (result.matches.length) {
        myLogFunc(result.matches.length + '张杀')
        // let matches = result.matches[result.matches.length-1]
        let p = result.first().point;
        if (p) {
            click(p.x, p.y);
            myLogFunc('选择杀')
            sleep(yanchi1)

            // 选敌军
            let thisImgs0 = images.read("./res/hx_diren.png");
            thisImgs0 = images.scale(thisImgs0, picJizhun, picJizhunHeight)
            // let p0 = findImage(captureScreen(), thisImgs0, { threshold: 0.85 });
            let result0 = images.matchTemplate(captureScreen(), thisImgs0, { region: [0, 0, deviceWidth, (deviceHeight / 2)], threshold: 0.85, max: 2 });
            log('敌人', result0)
            thisImgs0.recycle();
            if (result0.matches.length) {
                myLogFunc('选择敌人')
                let matchRc = result0.matches;
                for (let index = 0; index < matchRc.length; index++) {
                    if (index > 1) {
                        break;
                    }
                    click(matchRc[index].point.x + 80, matchRc[index].point.y + 100);
                    sleep(yanchi1)
                }
            }

            // 11111
            let thisImgs1 = images.read("./res/hx_queding.png");
            thisImgs1 = images.scale(thisImgs1, picJizhun, picJizhunHeight)
            let p1 = findImage(captureScreen(), thisImgs1, { threshold: 0.85 });
            thisImgs1.recycle();
            if (p1) {
                click(p1.x, p1.y)
                myLogFunc('确定')
                sleep(yanchi3)
            }

        }
    } else {
        myLogFunc('结束')
    }
}

function jieshuchupaiFunc() {
    let thisImgs = images.read("./res/hx_jieshuchupai.png");
    thisImgs = images.scale(thisImgs, picJizhun, picJizhunHeight)
    let p = findImage(captureScreen(), thisImgs, { threshold: 0.85 });
    thisImgs.recycle();
    if (p) {
        click(p.x, p.y)
        myLogFunc('结束出牌')
        sleep(yanchi1)
        // 弃牌流程 ***
        qipaiFunc()
        hx_xiadanFunc11()
    } else {
        sleep(yanchi2)
        myLogFunc("等待结束")
        let hx_baoxiang = images.read("./res/hx_baoxiang.png");
        hx_baoxiang = images.scale(hx_baoxiang, picJizhun, picJizhunHeight)
        let phx = findImage(captureScreen(), hx_baoxiang, { threshold: 0.85 });
        hx_baoxiang.recycle();
        if (phx) {
            click(phx.x, phx.y)
            myLogFunc('宝箱1')
            sleep(yanchi)
            fanhuiaFunc()
        } else {
            sleep(yanchi)
            fanhuiaFunc()
        }
    }
}

function fanhuiaFunc() {
    let hx_fanhui = images.read("./res/hx_fanhui.png");
    hx_fanhui = images.scale(hx_fanhui, picJizhun, picJizhunHeight)
    let pfh = findImage(captureScreen(), hx_fanhui, { threshold: 0.8 });
    hx_fanhui.recycle();
    if (pfh) {
        click(pfh.x, pfh.y)
        myLogFunc('返回')
        sleep(yanchi1)
        myLogFunc('重新开始')
        dianhanxinFunc()
    } else {
        sleep(yanchi1)
        jieshuchupaiFunc()
    }
}